<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('待办任务列表')"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            <label>流程信息：</label>
                            <input type="text" name="keyInfo"/>
                        </li>
                        <li>
                            <label>分派策略：</label>
                            <select name="taskPolicy" class="selectpicker"
                                    th:with="type=${@dict.getType('wfl_flowstep_task_policy')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                        th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <li>
                            <label>创建时间：</label>
                            <input type="text" class="time-input" placeholder="请选择创建时间"
                                   name="accepteTime"/>
                        </li>
                        <li>
                            <label>任务类型：</label>
                            <select name="taskType" class="selectpicker"
                                    th:with="type=${@dict.getType('busiflw_task_type')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                        th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="busiflw:waittask:export">
                <i class="fa fa-download"></i> 导出
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table" data-auto-refresh="true" data-auto-refresh-interval="30"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: bootstrap-table-auto-refresh-js"/>
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('busiflw:waittask:edit')}]];
    var removeFlag = [[${@permission.hasPermi('busiflw:waittask:remove')}]];
    var taskPolicyDatas = [[${@dict.getType('wfl_flowstep_task_policy')}]];
    var taskTypeDatas = [[${@dict.getType('busiflw_task_type')}]];
    var prefix = ctx + "busiflw/waittask";

    $(function () {
        var options = {
            url: prefix + "/list",
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export",
            modalName: "待办任务",
            columns: [{
                checkbox: true
            },
                {
                    field: 'taskId',
                    title: '任务编号'
                },
                {
                    field: 'versionId',
                    title: '流程版本号',
                    visible: false
                },
                {
                    field: 'instanceId',
                    title: '业务编号'
                },
                {
                    field: 'flowName',
                    title: '流程名称'
                },
                {
                    field: 'stepName',
                    title: '流程步骤'
                },
                {
                    field: 'belongName',
                    title: '归属人'
                },
                {
                    field: 'performerName',
                    title: '提交人'
                },
                {
                    field: 'taskPolicy',
                    title: '分派策略',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(taskPolicyDatas, value);
                    }
                },
                {
                    field: 'accepteTime',
                    title: '创建时间'
                },
                {
                    field: 'stopTime',
                    title: '结束时间'
                },
                {
                    field: 'taskType',
                    title: '任务类型',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(taskTypeDatas, value);
                    }
                },
                {
                    field: 'keyInfo',
                    halign: 'center',
                    title: '流程信息',
                    formatter: function (value, row, index) {
                        return getKeyInfo(row.keyInfo);
                    }
                },
                {
                    field: 'timeLimit',
                    title: '审批限时(小时)',
                    visible: false
                },
                {
                    field: 'waitTime',
                    title: '停留时间(小时)',
                    halign: 'center',
                    align: 'center',
                    formatter: function (value, row, index) {
                        return formatWaitTime(row);
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="dealWithWaitTask(\'' + row.versionId + '\',\'' + row.instanceId + '\',\'' + row.stepId + '\',\'' + row.taskId + '\',\'' + encodeURI(row.keyInfo) + '\',\'' + row.flowName + '\')"><i class="fa fa-edit"></i>处理</a> ');
                        //actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="submitTask(\'' + row.versionId + '\',\'' + row.instanceId + '\',\'' + row.stepId + '\',\'' + row.taskId + '\',\'' + encodeURI(row.keyInfo) + '\',\'' + row.flowName + '\')"><i class="fa fa-edit"></i>处理</a> ');
                        //actions.push('<a class="btn btn-info btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="approveList(\'' + row.instanceId + '\')"><i class="fa fa-list-ul"></i>审批记录</a> ');
                        if (row.taskType == 1) {
                            actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="remove(\'' + row.taskId + '\')"><i class="fa fa-remove"></i>删除</a>');
                        }
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });
    function dealWithWaitTask(versionId, instanceId, stepId, taskId, keyInfo, flowName) {
        $.ajax({
            type: 'POST',
            url: prefix + "/checkWaitTask",
            data: {
                "instanceId": instanceId,
                "taskId": taskId
            },
            success: function (result) {
                if (result.code == web_status.SUCCESS) {
                    var url = ctx + "businessFlow/dealWithWaitTask/" + versionId + "/" + instanceId + "/" + stepId + "/" + taskId + '?keyInfo=' + keyInfo;
                    $.modal.openTab(flowName, encodeURI(url));
                    // $.modal.reload();
                } else {
                    $.modal.alertError(result.msg);
                    $.table.refresh();
                }
            }
        });
    }
    function remove(taskId) {
        $.modal.confirm("确定要删除当前任务吗？", function () {
            $.ajax({
                type: 'POST',
                url: prefix + "/remove",
                data: {
                    "taskId": taskId
                },
                success: function (result) {
                    if (result.code == web_status.SUCCESS) {
                        $.modal.alertSuccess(result.msg);
                        $.table.refresh();
                    } else {
                        $.modal.alertError(result.msg);
                        $.table.refresh();
                    }
                }
            });
        })
    }

    /*处理任务等待时间*/
    function formatWaitTime(row) {
        if (row.timeLimit == 0) {
            return '<span style="color: green;font-size: larger">' + row.waitTime + '</span>';
        } else {
            if (row.waitTime > row.timeLimit) {
                return '<span style="color: red;font-size: larger">' + row.waitTime + '</span>';
            } else {
                return '<span style="color: green;font-size: larger">' + row.waitTime + '</span>';
            }
        }
    }


    /*流程信息json转标准页面格式*/
    function getKeyInfo(keyInfo) {
        const obj = JSON.parse(keyInfo);
        var keyInfoHtml = '<ul>'
        keyInfoHtml += '<li style = "text-align: left;" > 客户名称：';
        keyInfoHtml += '<span> ' + obj['cname'] + '</span>';
        keyInfoHtml += '</li>';
        keyInfoHtml += '</ul>';
        return keyInfoHtml;
    }


    /*任务提交*/
    function submitTask(versionId, instanceId, stepId, taskId, keyInfo) {
        var url = ctx + "businessFlow/approvaPage/" + versionId + "/" + instanceId + "/" + stepId + "/" + taskId + '?keyInfo=' + keyInfo;
        var options = {
            title: '选择客户',
            width: "500",
            height: "400",
            url: url,
            callBack: doSubmit
        };
        $.modal.openOptions(options);
    }

    function doSubmit(index, layero) {
        var body = $.modal.getChildFrame(index);
        var approveType = body.find('#approveType').val();
        var approveMemo = body.find('#approveMemo').val();
        var versionId = body.find('#versionId').val();
        var instanceId = body.find('#instanceId').val();
        var stepId = body.find('#stepId').val();
        var taskId = body.find('#taskId').val();
        var keyInfo = body.find('#keyInfo').val();
        $("#approveTypes").val(approveType);
        $("#approveMemos").val(approveMemo);
        if (approveType == "") {
            $.modal.msgWarning("请选择审批意见！");
            return false;
        }
        if (approveType != "T") {
            if (approveMemo == "") {
                $.modal.msgWarning("请填写意见备注！");
                return false;
            }
        }
        var index = top.layer.load(2, {shade: false});
        $.modal.disable();
        $.ajax({
            type: 'POST',
            url: ctx + "businessFlow/submitTask",
            async: false,
            data: {
                "versionId": versionId,
                "instanceId": instanceId,
                "stepId": stepId,
                "taskId": taskId,
                "keyInfo": keyInfo,
                "approveType": approveType,
                "approveMemo": approveMemo,
            },
            success: function (result) {
                if (result.code == web_status.SUCCESS) {
                    $.modal.alertConfirm(result.msg, function () {
                        $.modal.close(index);
                        $.modal.closeAll();
                        $.table.refresh();
                    });
                } else {
                    $.modal.alertWarning(result.msg);
                    $.modal.close(index);
                    $.modal.enable();
                }
            }
        });
        $.modal.close(index);
    }

    function approveList(instanceId) {
        $.modal.open("审批记录", ctx + "businessFlow/approveList/" + instanceId);
    }

</script>
</body>
</html>